package bosc.cn.controller;

import bosc.cn.constant.RetCdEnum;
import bosc.cn.service.SignService;
import cn.api.SignApi;
import cn.dto.SignRequest;
import cn.dto.SignResponse;
import cn.dto.VerifyRequest;
import cn.dto.VerifyResponse;
import cn.dto.common.CommonResponse;
import cn.dto.common.RequestWrapper;
import cn.dto.common.ResponseWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * @author tianshq
 * @date 2025/9/8 20:55
 * @description
 */

@Slf4j
@RestController
@RequestMapping("/payment/authorize")
public class SignController implements SignApi {

    @Autowired
    private SignService signService;

    //    sign001
    @Override
    @PostMapping("/sign")
    public ResponseWrapper<SignResponse> sign(@Valid @RequestBody RequestWrapper<SignRequest> request) {
        log.info("收到 bank 的 glblSrlNo: {}", request.getHead().getGlblSrlNo());
        ResponseWrapper<SignResponse> response = new ResponseWrapper<>();
        CommonResponse head = new CommonResponse();


        log.info("请求报文：{}", request.toString());
        SignResponse signResponse = null;
        try {
            signResponse = signService.sign(request.getBody());
        } catch (Exception e) {
            e.printStackTrace();
            response.setBody(signResponse);
            head.setRetCd(RetCdEnum.SYSTEM_ERROR.getCode());
            head.setRetMsg(RetCdEnum.SYSTEM_ERROR.getMsg());
            head.setRetTm(LocalDateTime.now());
            response.setHead(head);
            return response;
        }
        response.setBody(signResponse);
        head.setRetCd(RetCdEnum.SUCCESS.getCode());
        head.setRetMsg(RetCdEnum.SUCCESS.getMsg());
        head.setRetTm(LocalDateTime.now());
        response.setHead(head);
        log.info("响应报文：{}", response.toString());
        return response;
    }
    //    sign002
    @Override
    @PostMapping("/verify")
    public ResponseWrapper<VerifyResponse> verify(@Valid @RequestBody RequestWrapper<VerifyRequest> request) {
        ResponseWrapper<VerifyResponse> response = new ResponseWrapper<>();
        CommonResponse head = new CommonResponse();

        log.info("请求报文：{}", request.toString());
        VerifyResponse verifyResponse = null;
        try {
            verifyResponse = signService.verify(request.getBody());
        } catch (Exception e) {
            e.printStackTrace();
            response.setBody(verifyResponse);
            head.setRetCd(RetCdEnum.SYSTEM_ERROR.getCode());
            head.setRetMsg(RetCdEnum.SYSTEM_ERROR.getMsg());
            head.setRetTm(LocalDateTime.now());
            response.setHead(head);
            return response;
        }
        response.setBody(verifyResponse);
        head.setRetCd(RetCdEnum.SUCCESS.getCode());
        head.setRetMsg(RetCdEnum.SUCCESS.getMsg());
        head.setRetTm(LocalDateTime.now());
        response.setHead(head);
        log.info("响应报文：{}", response.toString());
        return response;
    }

}
